#include "currentampspectrum.h"
#include "private/spectrumprivatedefine.h"
#include "private/currentampspectrumprivatedefine.h"
#include "common/dataspecificationutils.h"
#include "common/binaryprocesshelper.h"
#include "common/xmldocument.h"
#include "private/spectrumdatafilecommondefine.h"
#include "common/xmlprocesshelper.h"
#include <QDebug>

namespace DataSpecificationGW
{

    static constexpr const int DATA_NAME_STR_LENGTH = 16;    // 电流数据名称长度
    static constexpr const int CURRENT_AMP_SPECTRUM_HEADER_LENGTH = 334;    // 电流图谱头信息长度（从图谱类型编码到测点编码）

    class CurrentAmpSpectrumPrivate
    {
    public:
        CurrentAmpExtInformationPrivate m_stCurrentAmpExtInformationPrivate;
        CurrentAmpDataPrivate m_stCurrentAmpDataPrivate;
    };

    CurrentAmpSpectrum::CurrentAmpSpectrum()
        : Spectrum(),
          m_pCurrentAmpSpectrumPrivate(new CurrentAmpSpectrumPrivate)
    {
        setSpectrumTypeCode(SPECTRUM_CODE_CURRENT_AMPLITUDE);
        m_pSpectrumPrivate->m_stSpectrumHeadPrivate.skipDetectionChannelID();
        m_pSpectrumPrivate->m_stSpectrumHeadPrivate.skipStorageDataType();
    }

    CurrentAmpSpectrum::~CurrentAmpSpectrum()
    {
    }

    /************************************************
     * 函数名   : setDataSpecificationVersion
     * 输入参数 :
       const DataSpecificationVersion eDataSpecificationVersion: 数据规范版本号
     * 输出参数 : NULL
     * 返回值   : void
     * 功能     : 设置数据规范版本号
     ************************************************/
    void CurrentAmpSpectrum::setDataSpecificationVersion(const DataSpecificationVersion eDataSpecificationVersion)
    {
        Spectrum::setDataSpecificationVersion(eDataSpecificationVersion);
    }

    /************************************************
     * 函数名   : spectrumName
     * 输入参数 : NULL
     * 输出参数 : NULL
     * 返回值   : QString
     * 功能     : 图谱名称
     ************************************************/
    QString CurrentAmpSpectrum::spectrumName() const
    {
        return STR_FILE_NODE_CURRENT_AMP;
    }

    /************************************************
     * 函数名   : setCurrentAmpExtInformation
     * 输入参数 :
       const CurrentAmpExtInformation& stCurrentAmpExtInformation: Current幅值ExtInformation
     * 输出参数 : NULL
     * 返回值   : void
     * 功能     : 设置Current幅值ExtInformation
     ************************************************/
    void CurrentAmpSpectrum::setCurrentAmpExtInformation(const CurrentAmpExtInformation& stCurrentAmpExtInformation)
    {
        m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit = stCurrentAmpExtInformation.eAmpUnit;
        m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount = stCurrentAmpExtInformation.ucCurrentDataCount;
    }

    /************************************************
     * 函数名   : setCurrentAmpData
     * 输入参数 :
       const CurrentAmpData& stCurrentAmpData: Current幅值Data
     * 输出参数 : NULL
     * 返回值   : void
     * 功能     : 设置Current幅值Data
     ************************************************/
    void CurrentAmpSpectrum::setCurrentAmpData(const CurrentAmpData& stCurrentAmpData)
    {
        m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas = stCurrentAmpData.currentDatas;
    }

    /************************************************
     * 函数名   : getCurrentAmpExtInformation
     * 输入参数 : NULL
     * 输出参数 :
       CurrentAmpExtInformation& stCurrentAmpExtInformation: Current幅值ExtInformation
     * 返回值   : void
     * 功能     : 获取Current幅值ExtInformation
     ************************************************/
    void CurrentAmpSpectrum::getCurrentAmpExtInformation(CurrentAmpExtInformation& stCurrentAmpExtInformation)
    {
        stCurrentAmpExtInformation.eAmpUnit = static_cast<AmpUnit>(m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit);
        stCurrentAmpExtInformation.ucCurrentDataCount = m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount;
    }

    /************************************************
     * 函数名   : getCurrentAmpData
     * 输入参数 : NULL
     * 输出参数 :
       CurrentAmpData& stCurrentAmpData: Current幅值Data
     * 返回值   : void
     * 功能     : 获取Current幅值Data
     ************************************************/
    void CurrentAmpSpectrum::getCurrentAmpData(CurrentAmpData& stCurrentAmpData)
    {
        stCurrentAmpData.currentDatas = m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas;
    }

    /************************************************
     * 函数名   : saveBinarySpectrumExtInfo
     * 输入参数 :
       QDataStream& out: 输出流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存二进制可扩展信息段
     ************************************************/
    bool CurrentAmpSpectrum::saveBinarySpectrumExtInfo(QDataStream& out)
    {
        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipAmpUnit) out << m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit;
        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipCurrentDataCount) out << m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount;

        return true;
    }

    /************************************************
     * 函数名   : saveXMLSpectrumExtInfo
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
       QDomElement& element: dom元素
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存XML可扩展信息段
     ************************************************/
    bool CurrentAmpSpectrum::saveXMLSpectrumExtInfo(XMLDocument& xmlDocumentObj, QDomElement& element)
    {
        xmlDocumentObj.beginElement(element);
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_EXTINFORMATION);

        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipAmpUnit) xmlDocumentObj.setValue(TEXT_AMP_UNIT, QString::number(m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit));
        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipCurrentDataCount) xmlDocumentObj.setValue(TEXT_CURRENT_DATA_COUNT, QString::number(m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount));

        return true;
    }

    /************************************************
     * 函数名   : saveJSONSpectrumExtInfo
     * 输入参数 :
       rapidjson::Document::AllocatorType& alloc:
       rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存JSON可扩展信息段
     ************************************************/
    bool CurrentAmpSpectrum::saveJSONSpectrumExtInfo(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& jsonValue)
    {
        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipAmpUnit) saveJSONField(m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit, TEXT_AMP_UNIT, alloc, jsonValue);
        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipCurrentDataCount) saveJSONField(m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount, TEXT_CURRENT_DATA_COUNT, alloc, jsonValue);

        return true;
    }

    /************************************************
     * 函数名   : saveBinarySpectrumData
     * 输入参数 :
       QDataStream& out: 输出流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存二进制图谱数据段
     ************************************************/
    bool CurrentAmpSpectrum::saveBinarySpectrumData(QDataStream& out)
    {
        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bSkipcurrentDatas)
        {
            auto i = m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas.constBegin();
            while (i != m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas.constEnd())
            {
                DataSpecificationUtils::convertStringToBinary(i.key(), TYPE_UNICODE, out, DATA_NAME_STR_LENGTH);
                out << i.value();
                ++i;
            }
        }

        DataSpecificationUtils::saveBinaryReservedData(SPECTRUM_BASE_LENGTH - out.device()->size(), out);

        return true;
    }

    /************************************************
     * 函数名   : saveXMLSpectrumData
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
       QDomElement& element: dom元素
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存XML图谱数据段
     ************************************************/
    bool CurrentAmpSpectrum::saveXMLSpectrumData(XMLDocument& xmlDocumentObj, QDomElement& element)
    {
        xmlDocumentObj.beginElement(element);
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_DATA);

        //if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bSkipcurrentDatas) xmlDocumentObj.setValue(TEXT_Current_AMP, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas);

        return true;
    }

    /************************************************
     * 函数名   : saveJSONSpectrumData
     * 输入参数 :
       rapidjson::Document::AllocatorType& alloc:
       rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存JSON图谱数据段
     ************************************************/
    bool CurrentAmpSpectrum::saveJSONSpectrumData(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& jsonValue)
    {
        //if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bSkipcurrentDatas) saveJSONField(m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas, TEXT_Current_AMP, alloc, jsonValue);
        return true;
    }

    /************************************************
     * 函数名   : parseBinarySpectrumExtInfo
     * 输入参数 :
       QDataStream& in: 输入流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析二进制可扩展信息段
     ************************************************/
    bool CurrentAmpSpectrum::parseBinarySpectrumExtInfo(QDataStream& in)
    {
        if (!in.device()->seek(CURRENT_AMP_SPECTRUM_HEADER_LENGTH))
        {
            qDebug() << "CurrentAmpSpectrum::parseBinarySpectrumExtInfo failed";
            return false;
        }

        parseBinaryField(in, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipAmpUnit, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bHasAmpUnit);
        parseBinaryField(in, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bSkipCurrentDataCount, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bHasCurrentDataCount);

        return in.status() == QDataStream::Ok;
    }

    /************************************************
     * 函数名   : parseXMLSpectrumExtInfo
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析XML可扩展信息段
     ************************************************/
    bool CurrentAmpSpectrum::parseXMLSpectrumExtInfo(XMLDocument& xmlDocumentObj)
    {
        xmlDocumentObj.beginElement(spectrumName());
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_EXTINFORMATION);

        parseXMLField(xmlDocumentObj, TEXT_AMP_UNIT, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bHasAmpUnit);
        parseXMLField(xmlDocumentObj, TEXT_CURRENT_DATA_COUNT, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bHasCurrentDataCount);

        xmlDocumentObj.endElement();
        xmlDocumentObj.endElement();

        return true;
    }

    /************************************************
     * 函数名   : parseJSONSpectrumExtInfo
     * 输入参数 :
       const rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析JSON可扩展信息段
     ************************************************/
    bool CurrentAmpSpectrum::parseJSONSpectrumExtInfo(const rapidjson::Value& jsonValue)
    {
        parseJSONField(jsonValue, TEXT_AMP_UNIT, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucAmpUnit, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bHasAmpUnit);
        parseJSONField(jsonValue, TEXT_CURRENT_DATA_COUNT, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.bHasCurrentDataCount);

        return true;
    }

    /************************************************
     * 函数名   : parseBinarySpectrumData
     * 输入参数 :
       QDataStream& in: 输入流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析二进制图谱数据段
     ************************************************/
    bool CurrentAmpSpectrum::parseBinarySpectrumData(QDataStream& in)
    {
        if (!in.device()->seek(CURRENT_AMP_DATA_START_POS))
        {
            qDebug() << "CurrentAmpSpectrum::parseBinarySpectrumData failed";
            return false;
        }

        if (!m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bSkipcurrentDatas)
        {
            for (int i = 0; i < m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount; ++i)
            {
                QString qstrDataName;
                float fCurrentData;
                bool bExist;
                parseBinaryField(in, false, DataSpecificationGW::TYPE_UNICODE, DATA_NAME_STR_LENGTH, qstrDataName, bExist);

                in >> fCurrentData;

                m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas.insert(qstrDataName, fCurrentData);
            }

            if (m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpExtInformationPrivate.ucCurrentDataCount > 0)
            {
                m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bHascurrentDatas = true;
            }
            else
            {
                m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bHascurrentDatas = false;
            }

        }

        return in.status() == QDataStream::Ok;
    }

    /************************************************
     * 函数名   : parseXMLSpectrumData
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析XML图谱数据段
     ************************************************/
    bool CurrentAmpSpectrum::parseXMLSpectrumData(XMLDocument& xmlDocumentObj)
    {
        xmlDocumentObj.beginElement(spectrumName());
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_DATA);

        //parseXMLField(xmlDocumentObj, TEXT_Current_AMP, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.currentDatas, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bHascurrentDatas);

        xmlDocumentObj.endElement();
        xmlDocumentObj.endElement();

        return true;
    }

    /************************************************
     * 函数名   : parseJSONSpectrumData
     * 输入参数 :
       const rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析JSON图谱数据段
     ************************************************/
    bool CurrentAmpSpectrum::parseJSONSpectrumData(const rapidjson::Value& jsonValue)
    {
        //parseJSONField(jsonValue, TEXT_Current_AMP, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.fcurrentDatas, m_pCurrentAmpSpectrumPrivate->m_stCurrentAmpDataPrivate.bHascurrentDatas);

        return true;
    }

}
